package cn.com.kblog.util;


import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Properties;
import java.util.Map.Entry;


import com.jfinal.kit.PathKit;
import com.jfinal.log.Logger;

/**
 * APP配置获得工具
 * @author KK.Luo
 * Email luokai8@vip.qq.com
 * @date 2013-12-2下午12:59:41
 */
public class AppConfigUtil {
private static final HashMap<String, String> config = new HashMap<String, String>();
private static Properties properties;   
private static Logger logger = Logger.getLogger(AppConfigUtil.class);
static{
	//默认的properties
	InputStream inStream;
	try {
		
		inStream = new FileInputStream(new File(PathKit.getRootClassPath() + File.separator + "app.properties"));
		properties = new Properties();
		properties.load(inStream);
		for (Entry<Object, Object> entry : properties.entrySet()) {
            config.put(entry.getKey().toString(), entry.getValue().toString());
        }
	}  catch (FileNotFoundException e) {
		e.printStackTrace();
		throw new IllegalArgumentException("Properties file not found: " + "classes/app.properties");
	} catch (IOException e) {
		e.printStackTrace();
		throw new IllegalArgumentException("Properties file can not be loading: " + "classes/app.properties");
	}
	
}
    /**
     * 读取配置文件，将配置文件分配到一个静态HashMap
     * @param properties
     * @return
     */
    public static void loadConfig(Properties properties){
        for (Entry<Object, Object> entry : properties.entrySet()) {
            config.put(entry.getKey().toString(), entry.getValue().toString());
        }
    }
    
    public static final String get(String key) {
        return config.get(key);
    }
    
    public static Boolean getToBoolean(String key) {
        return Boolean.parseBoolean(config.get(key));
    }
    
    public static Long getToLong(String key) {
        return Long.parseLong(config.get(key));
    }
}
